/* main.c */
#include "app_inc.h"

SDC_Info_T SDCardInfoStruct;

#define SDC_MUL_BLOCK_CNT  (2U)
static uint8_t gRxBuffer[SDC_BLOCK_SIZE_BYTE*SDC_MUL_BLOCK_CNT];
static uint8_t gTxBuffer[SDC_BLOCK_SIZE_BYTE*SDC_MUL_BLOCK_CNT];

static bool DemoCardSingleRW(void);
static bool DemoCardMultpleRW(void);

int main(void)
{
    uint8_t ch;
    bool bRet;
    //uint32_t i;
    //uint32_t blkIdx = 0u;
    
    BSP_InitSystem();
    BSP_InitStdioLPUART(115200U);

    /* SDCard. */
    BSP_InitExt_SDCard(); /* clock & pins. */
    BSP_SDC_Install();

    printf("\r\n\r\nSDCard Example.\r\n");
    printf("Compiled on %s, %s\r\n\r\n", __DATE__, __TIME__);

    
    bRet = SDC_InitCard(&SDCardInfoStruct);
    if (!bRet)
    {
        printf("BAD.\r\n");
        while (1);
    }
    printf("GOOD.\r\n");

    printf("CardType  : %d\r\n", SDCardInfoStruct.CardType);
    printf("CardBlkCnt: %d MB\r\n", SDCardInfoStruct.CardBlkCnt/2u/1024u);

    printf("# DemoCardSingleRW() ...\r\n");
    if( !DemoCardSingleRW() )
    {
        printf("BAD.\r\n");
    }
    else
    {
        printf("GOOD.\r\n");
    }
    
    printf("# DemoCardMultpleRW() ...\r\n");
    if ( !DemoCardMultpleRW() )
    {
        printf("BAD.\r\n");
    }
    else
    {
        printf("GOOD.\r\n");
    }
    
    while (1)
    {
        ch = getchar();
        putchar(ch);
    }
}

static bool DemoCardSingleRW(void)
{
    uint32_t blkIdx;
    uint8_t blkData;
    uint8_t tmp8;
    uint32_t i;

    printf("DemoCardSingleRW() start ...\r\n");

    do
    {
        /* Write. */
        printf("Input the writting block index: ");
        scanf("%d", &blkIdx);
        printf("\r\nInput the writting character: ");
        scanf("%c", &blkData);
        printf("\r\nWrite single block %d with data(ASCII) 0x%X\r\n", blkIdx, blkData);
        printf("Press any key to continue ...\r\n");
        getchar();
        
        for (i = 0U; i < SDC_BLOCK_SIZE_BYTE; i++)
        {
            gTxBuffer[i] = blkData;
        }
        if ( !SDC_WriteBlock(blkIdx, gTxBuffer) )
        {
            printf("Write error.\r\n");
        }

        /* Read. */
        printf("Input the reading block index: ");
        scanf("%d", &blkIdx);
        printf("\r\nRead single block %d\r\n", blkIdx);
        if ( !SDC_ReadBlock(blkIdx, gRxBuffer) )
        {
            printf("Read error.\r\n");
        }
        else
        {
            /* printf reading data. */
            for (i = 0U; i < SDC_BLOCK_SIZE_BYTE; i++)
            {
                if (0U == i%8U)
                {
                    printf("\r\n%4d : ", i);
                }
                printf(" 0x%2X ", gRxBuffer[i]);
            }
            printf("\r\n");
        }

        /* Try again. */
        printf("Try again? [Y/N]: ");
        scanf("%c", &tmp8);
        printf("\r\n");
    } while (tmp8 == 'y' || tmp8 == 'Y');

    printf("DemoCardSingleRW() end.\r\n");

    return true;
}

static bool DemoCardMultpleRW(void)
{
    uint32_t blkIdx;
    uint8_t blkData;
    uint8_t tmp8;
    uint32_t i;

    printf("DemoCardMultpleRW() start ...\r\n");

    do
    {
        /* Write. */
        printf("Input the first writting block index: ");
        scanf("%d", &blkIdx);
        printf("\r\nInput the writting character: ");
        scanf("%c", &blkData);
        printf("\r\nWrite blocks %d-%d with data(ASCII) 0x%X\r\n", blkIdx, blkIdx+SDC_MUL_BLOCK_CNT-1U, blkData);

        for (i = 0U; i < SDC_BLOCK_SIZE_BYTE*SDC_MUL_BLOCK_CNT; i++)
        {
            gTxBuffer[i] = blkData;
        }
        if ( 0U != SDC_WriteBlocks(blkIdx, SDC_MUL_BLOCK_CNT, gTxBuffer) )
        {
            printf("Write error.\r\n");
        }

        /* Read. */
        printf("Input the first reading block index: ");
        scanf("%d", &blkIdx);
        printf("\r\nRead blocks %d-%d\r\n", blkIdx, blkIdx+SDC_MUL_BLOCK_CNT-1U);
        if ( 0U != SDC_ReadBlocks(blkIdx, SDC_MUL_BLOCK_CNT, gRxBuffer) )
        {
            printf("Read error.\r\n");
        }
        else
        {
            /* printf reading data. */
            for (i = 0U; i < SDC_BLOCK_SIZE_BYTE*SDC_MUL_BLOCK_CNT; i++)
            {
                if (0U == i%8U)
                {
                    printf("\r\n%4d : ", i);
                }
                printf(" 0x%2X ", gRxBuffer[i]);
            }
            printf("\r\n");
        }

        /* Try again. */
        printf("Try again? [Y/N]: ");
        scanf("%c", &tmp8);
        printf("\r\n");
    } while (tmp8 == 'y' || tmp8 == 'Y');

    printf("DemoCardMultpleRW() end.\r\n");

    return true;
}

/* EOF. */

